#include <stdlib.h>
#include <math.h>
#include <cairo.h>
#include <clutter/clutter.h>

#define UNUSED(x) (void)x 

double coordx[10000];
double coordy[10000];
int count = 0;

/* 这是Callback函数 */ 
static gboolean on_stage_button_press (ClutterStage *stage, ClutterEvent *event, gpointer data)
{

    UNUSED(stage);
    UNUSED(data);
    float x = 0;
    float y = 0;
    clutter_event_get_coords (event, &x, &y);
    if ( event->type == CLUTTER_BUTTON_PRESS )
    {
        coordx[count] = (int)x;
        coordy[count++] = (int)y;
        printf("count=%d x=%d y=%d\n", count, (int)x, (int)y);
    }
    return TRUE; /* Stop further handling of this event. */ 
}


static gboolean draw_clock(ClutterCanvas *canvas,
           cairo_t *cr, int width, int height)
{
    int j;
    ClutterColor color;

    UNUSED(width);
    UNUSED(height);
    UNUSED(canvas);

    cairo_save (cr);
    /* clear the contents of the canvas, to avoid painting
    * over the previous frame
    */
    cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
    cairo_paint (cr);
    cairo_restore (cr);

    //设置 画笔的颜色和宽度, (蓝色,宽1)
    color = *CLUTTER_COLOR_Red;
    color.alpha = 128;
    clutter_cairo_set_source_color (cr, &color);
    cairo_set_line_width (cr, 2);
    
    //每两个点之间画一条直线
    for ( j  = 0; j <= count -1; j++ )
    {
        if( j == 0 )
        {
            //用于设置线段起点
            cairo_move_to(cr, coordx[j], coordy[j]);  
        }
        else
        {
            //用于设置线段终点
            cairo_line_to(cr, coordx[j], coordy[j]);    
        }
    }
    
    //函数会将 cr 中的路径绘制出来
    cairo_stroke(cr);
    /* we're done drawing */
    return TRUE;
}

static gboolean display(gpointer canvas)
{
    /* invalidate the contents of the canvas */
    clutter_content_invalidate(canvas);
    /* keep the timeout source */
    return G_SOURCE_CONTINUE;
}


static void on_actor_resize (ClutterActor           *actor,
                 const ClutterActorBox  *allocation,
                 ClutterAllocationFlags  flags,
                 gpointer                user_data)
{
    UNUSED(user_data);
    UNUSED(flags);
    UNUSED(allocation);
    
    float width, height;
    /* match the canvas size to the actor's */
    clutter_actor_get_size (actor, &width, &height);
    clutter_canvas_set_size (CLUTTER_CANVAS (clutter_actor_get_content (actor)),
                            ceilf (width),
                            ceilf (height));
}

int main(int argc, char *argv[])
{
    ClutterActor *stage, *actor;
    ClutterContent *canvas;

    /* initialize Clutter */
    if (clutter_init(&argc, &argv) != CLUTTER_INIT_SUCCESS)
        return EXIT_FAILURE;

    /* create a resizable stage */
    stage = clutter_stage_new();
    clutter_stage_set_title(CLUTTER_STAGE(stage), "2D Drawing");
    clutter_stage_set_user_resizable(CLUTTER_STAGE(stage), TRUE);
    clutter_actor_set_background_color(stage, CLUTTER_COLOR_LightSkyBlue);
    clutter_actor_set_size(stage, 300, 300);
    clutter_actor_show(stage);
    g_signal_connect (stage, "button-press-event",G_CALLBACK (on_stage_button_press), NULL);

    /* our 2D canvas, courtesy of Cairo */
    canvas = clutter_canvas_new();
    clutter_canvas_set_size(CLUTTER_CANVAS(canvas), 300, 300);

    actor = clutter_actor_new();
    clutter_actor_set_content(actor, canvas);
    clutter_actor_set_content_scaling_filters(actor,
         CLUTTER_SCALING_FILTER_TRILINEAR, CLUTTER_SCALING_FILTER_LINEAR);
    clutter_actor_add_child(stage, actor);

    g_signal_connect (actor, "allocation-changed", G_CALLBACK (on_actor_resize), NULL);

    /* the actor now owns the canvas */
    g_object_unref(canvas);

    /* bind the size of the actor to that of the stage */
    clutter_actor_add_constraint(actor, clutter_bind_constraint_new(stage, CLUTTER_BIND_SIZE, 0));

    /* quit on destroy */
    g_signal_connect(stage, "destroy", G_CALLBACK(clutter_main_quit), NULL);

    /* connect our drawing code */
    g_signal_connect(canvas, "draw", G_CALLBACK(draw_clock), NULL);

    /* set up a timer that invalidates the canvas every second */
    clutter_threads_add_timeout(33, display, canvas);

    clutter_main();

    return EXIT_SUCCESS;
}
